package com.miyu.client1.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class javatest02 {
    public static void main(String[] args) {
        String dt="202101";
        String pattern ="yyyyMM";
        int offset=-1;
        Calendar cal = Calendar.getInstance();
        cal.setTime(stringToDate(dt, pattern));
        cal.add(Calendar.MONTH, offset);
        System.out.println(dateToString(cal.getTime(), pattern));
    }

    public static Date stringToDate(String s, String pattern)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date d = null;

        try {
            d = sdf.parse(s);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return d;
    }

    /**
     *  将 Date 类型期转换为字符串类型日期
     *
     * @return
     */
    public static String dateToString(Date d, String pattern)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);

        return sdf.format(d);
    }

}
